package pers.liu.yibandemo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.liu.yibandemo.entity.Admin;
import pers.liu.yibandemo.mapper.AdminMapper;

import java.util.List;
import java.util.Random;

@Service
public class AdminService extends ServiceImpl<AdminMapper, Admin> {

    @Autowired
    private AdminMapper adminMapper;

    // 根据管理员名称查询管理员信息
    public Admin getAdminByName(String adminName) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_name", adminName);
        return adminMapper.selectOne(queryWrapper);
    }
    // 根据管理员name和password查询
    public boolean getAdmin(Admin admin){
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Admin> eq = queryWrapper.eq("admin_name", admin.getAdminName())
                .eq("admin_password", admin.getAdminPassword());
        Admin one = adminMapper.selectOne(eq);
        return one != null;
    }
    //随便查询一名管理员以接收离线消息
    public Admin getOneAdmin() {
        List<Admin> adminList = adminMapper.selectList(null);
        if (adminList.isEmpty()) {
            return null; // 没有管理员可供选择
        }

        // 使用随机数生成器生成一个随机索引
        Random random = new Random();
        int randomIndex = random.nextInt(adminList.size());

        // 返回随机选择的管理员
        return adminList.get(randomIndex);
    }
}
